import os
import sys
sys.path.insert(1, os.path.join(sys.path[0], '..')) # for upper files visibility


import streamlit as st
from globals import APP_NAME, MAIN_PAGE_NAME, MAIN_KEY_BASE, FAVICON, LOGO
from database.queries.table_orm import SynOrmTableQueries
from database.queries.physical_activity_orm import SynOrmPhysicalActivityQueries
from database.queries.purpose_orm import SynOrmPurposeQueries
from widgets.authenticator import Authenticator
from widgets.menu import menu
from widgets.diet_compiler import diet_compiler

    
st.set_page_config(
    page_title=f"{APP_NAME} • {MAIN_PAGE_NAME}", 
    page_icon=FAVICON, 
    initial_sidebar_state="collapsed",
)


# # for db testing
# @st.cache_data
# def execute_test_queries():
#     SynOrmTableQueries.drop_tables()
#     SynOrmTableQueries.create_tables()
#     SynOrmPurposeQueries.insert_purposes()
#     SynOrmPhysicalActivityQueries.insert_physical_activities()
#     SynOrmUserQueries.test_insert_users()
# execute_test_queries()

# Creating tables if not exists:
@st.cache_data
def create_tables_on_init():
    SynOrmTableQueries.create_tables()
    SynOrmPurposeQueries.insert_purposes()
    SynOrmPhysicalActivityQueries.insert_physical_activities()
create_tables_on_init()

# Sidebar menu
menu()

if "authenticator" not in st.session_state:
    st.session_state["authenticator"] = Authenticator()
authenticator: Authenticator = st.session_state["authenticator"]

if not authenticator.is_authorized():
    with st.columns([1, 5, 1])[1]:
        st.image(LOGO)
    authenticator.authorization_form()
else:
    diet_compiler(key_base=MAIN_KEY_BASE)
